package com.paypalpay.controller.interfaces;

import com.alibaba.fastjson.JSON;
import com.paypalpay.entity.common.DeviceTypeSelecte;
import com.paypalpay.external.entity.selectdevicetype.response.Data;
import com.paypalpay.external.entity.selectdevicetype.response.ResopnseSelectDeviceType;
import com.paypalpay.external.entity.selectdevicetype.response.TradeData;
import com.paypalpay.service.DeviceTypeSelectedService;
import com.paypalpay.service.SequenceService;
import com.paypalpay.util.wx.MD5;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Demo 微信用户绑定，普通用户就不用绑定了，但是还是要入库
 *
 * @author lm
 * @date 2016/09/06
 */
@RestController
@Api(value = "获取用户前端设备类型下拉选框值的接口")
public class SelectDeviceTypeController {
    @Autowired
    private DeviceTypeSelectedService deviceTypeSelectedService;
    @Autowired
    HttpSession session;
    @Autowired
    SequenceService sequenceService;

    private String token = "yv8JXeTTfqyedLcU2LAqHUTKtYA4G1kz";

    private String tradeType = "F0011";

    @CrossOrigin
    @RequestMapping(value = "/selectDeviceType", produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    @ResponseBody
    public String selectDeviceType(HttpServletRequest request) {

        String lang=request.getParameter("lang");
        // 1，生成流水号
        String ConnSeqNo = sequenceService.getPackageTaskNo();
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tradeTime = df.format(new Date());

        //取出数据从数据库
        List<DeviceTypeSelecte> typeList = deviceTypeSelectedService.queryDeviceTypeList();
        List<TradeData> tradeData = new ArrayList<TradeData>();
        TradeData tra = null;
        if (typeList == null || typeList.size() <= 0) {
            System.out.println("未取到typeList数据：" + typeList);
        } else {
            for (DeviceTypeSelecte deviceType : typeList) {

                String deviceTypeText = deviceType.getDeviceTypeText();
                String deviceTypeTextEN=deviceType.getDeviceTypeTextEN();
                String deviceTypeValue = deviceType.getDeviceTypeValue();

                tra = new TradeData();
                if(lang.equals("cn")){
                    tra.setText(deviceTypeText);
                }else{
                    tra.setText(deviceTypeTextEN);
                }

                tra.setValue(deviceTypeValue);
                tradeData.add(tra);
            }

            Data data = new Data();
            data.setConnSeqNo(ConnSeqNo);
            data.setToken(token);
            data.setTradeType(tradeType);
            data.setTradeTime(tradeTime);
            data.setTradeRstCode(1000);
            data.setTradeRstMessage("成功");
            data.setTradeData(tradeData);

            ResopnseSelectDeviceType resopnseSelectDeviceType = new ResopnseSelectDeviceType();
            resopnseSelectDeviceType.setData(data);
            resopnseSelectDeviceType.setSign(MD5.calcMD5(JSON.toJSONString(data)));

            // 返回给调用者的报文
            String jsonString = JSON.toJSONString(resopnseSelectDeviceType);
            System.out.println("响应报文：" + jsonString);
            return jsonString;
        }
        return  null;
    }
}
